<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\MenuModel;
use app\model\ReRoleModel;
use app\model\RoleModel;
use think\Db;
use think\Validate;

class Role extends Base
{
    /**
     * 列表页
     * @return \think\response\View
     */
    public function roleList()
    {
        return view();
    }

    /**
     * 添加 编辑页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveRoleInfo(){
        $params = $this->request->param();
        $roleModel = new RoleModel();

        if(!empty($params['id'])){
            $info=$roleModel->where(['id'=>$params['id']])->find();
            $this->assign('info',$info);
        }
        return view();
    }

    public function ApiSaveRoleInfo(){
        $params = $this->request->param();
        $rule = [
            'name|角色名称'=>'require|max:20',
//            'bank_id|银行名称'=>'require|number',
            'desc|备注'=>'require',
            'status|状态'=>'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            Helper::returnJson(['code'=>400,'msg'=>$validate->getError()]);
        }

        $RoleModel = new RoleModel();
        $data = [
            'name' => $params['name'],
//            'bank_id' => $params['bank_id'],
            'desc' => $params['desc'],
            'status' => $params['status'],
        ];

        if(!empty($params['id'])){
            $res = $RoleModel->where(['id'=>$params['id']])->update($data);
        }else{
            $res = $RoleModel->insert($data);
        }

        if($res !== false){
            Helper::returnJson(['code'=>200,'msg'=>'更新成功','data'=>$res]);
        }else{
            Helper::returnJson(['code'=>400,'msg'=>'更新失败，请重新操作！','data'=>$res]);
        }
    }





    /**
     * 列表接口
     */
    public function get()
    {
        $param = $this->request->param();

        $page = isset($params['page']) ? intval($param['page']) : 1;
        $pageSize = isset($params['limit']) ? intval($param['limit']) : 10;

        $RoleModel = new RoleModel();
        $arr = ['1'=>'role.bank_id','like'=>'role.name'];
        $where = $this->select_param($arr,$param);

        $list = $RoleModel->getRolePge($page,$pageSize,$where,false);
        $count = $RoleModel->getRolePge(0,0,$where,true);

        Helper::returnJson(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }


    /**
     * 渲染菜单数据
     * @return mixed
     */
    public function saveRoleConfigInfo(){
        $params = $this->request->param();
        $role_id = $params['id'];
        $menuModel = new MenuModel();
        $menuList = $menuModel->getRoleMenuList($role_id,['menu.menu_id'=> 0 ]);
        foreach ($menuList as $key=>&$value){
            $menuChild = $menuModel->getRoleMenuList($role_id,['menu.menu_id'=>$value['id']]);
            $value['child'] = $menuChild;
            $value['is_checked'] = !empty($value['is_menu']) ? 1 : 0;
        }
        $this->assign('menuList',$menuList);
        $this->assign('role_id',$role_id);
        return view();
    }

    /**
     * 保存角色配置
     */
    public function ApiSaveRoleConfig(){
        $params = $this->request->param();
        $RoleMenuModel = new ReRoleModel();

        $data = [];
        foreach ($params['menu_id'] as $item){
            $data[]=[
                'role_id'=>$params['role_id'],
                'menu_id'=>$item,
            ];
        }
        // 启动事务
        Db::startTrans();
        try{
            $RoleMenuModel->where(['role_id'=>$params['role_id']])->delete();
            $RoleMenuModel->insertAll($data);
            // 提交事务
            Db::commit();
            Helper::returnJson(['code'=>200,'msg'=>'配置成功','data'=>[]]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            Helper::returnJson(['code'=>400,'msg'=>'配置失败','data'=>[]]);
        }
    }

}